package com.ifast.common.utils;


import java.security.MessageDigest;

public class MD5 {
	private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

	/**
	 * 转换字节数组丿6进制字符丿
	 *
	 * @param b 字节数组
	 * @return 16进制字符丿
	 */
	private static String byteArrayToHexString(byte[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			sb.append(byteToHexString(b[i]));
		}

		return sb.toString();
	}

	/**
	 * 转换字节数为16进制字符丿
	 *
	 * @param b byte数忍
	 * @return 16进制字符丿
	 */
	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0) {
			n = 256 + n;
		}
		int d1 = n / 16;
		int d2 = n % 16;

		return hexDigits[d1] + hexDigits[d2];
	}

	/**
	 * 获取指定字符串的MD5编码
	 *
	 * @param original 字符丿
	 * @return MD5编码
	 */
	public static String MD5Encode(String original) {
		String ret = null;

		try {
			ret = new String(original);
			MessageDigest md = MessageDigest.getInstance("MD5");
			ret = byteArrayToHexString(md.digest(ret.getBytes()));
		} catch (Exception ex) {
			// empty
		}

		return ret;
	}

	public static void main(String[] args) {
		
		System.out.println(MD5.MD5Encode("1"));
		
	}

}
